package com.example.demo;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.io.IOException;
import java.util.Map;

/**
 * 类描述：项目注册加密
 */
public class RegisterKey implements ServletContextListener {
/*
    *//**
     * 文件路径
     *//*
    private static String projectPath = new Object() {
        public String getPath() {
            //  return this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();//this.getClass().getResource("/").getPath();
            String jarFilePath = RegisterKey.class.getProtectionDomain().getCodeSource().getLocation().getFile();
// URL Decoding
            try {
                jarFilePath = java.net.URLDecoder.decode(jarFilePath, "UTF-8");
                System.out.println(jarFilePath);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            return jarFilePath;
        }
    }.getPath();*/
    /**
     * 文件名
     */
    private static String fileName = "register.properties";
    /**
     * 加密前缀
     */
    private static String lock = "";
    /**
     * 自定义 key
     */
    private static String key = "";

    private static Map<String,String> map;

    private static FileWrite fileWrite;

    public RegisterKey() throws IOException {
         this.fileWrite = new FileWrite(fileName);
         this.map =  fileWrite.PrMap;
         this.lock = map.get("lock");
         this.key = map.get("key");
         this.writeFile();
    }

    /**
     * 描述：验证注册是否正确
     *
     * @return
     */
    public boolean isRegister() {
        //配置文件MKEY
        String mkey = map.get("MKEY");
        DesUtils des = null;
        try {
            des = new DesUtils(key);
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        String mackey = "";
        try {
            mackey = des.decrypt(mkey);//解密
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        String mackeyr = lock + mackey;
        String mKey = "";
        try {
            mKey = des.encrypt(mackeyr);//加密
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        //获取注册码
        String aKey = map.get("AKEY");

        if (mKey.equals(aKey)) {
            System.out.println("验证注册成功");
            return true;
        } else {
            System.out.println("验证注册失败");
            return false;
        }
    }

    /* (non-Javadoc)
     * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
     * 消亡的时候执行的方法
     */
    public void contextDestroyed(ServletContextEvent arg0) {
        // TODO Auto-generated method stub

    }

    /* (non-Javadoc)
     * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
     * 项目在启动初始化的时候就会执行
     */
   public void contextInitialized(ServletContextEvent arg0) {
      /*   // TODO Auto-generated method stub
        ReadFile file = new ReadFile(projectPath, fileName);
        String mac = MacUtil.getMacAddress();
        DesUtils des = null;
        String mKey = "";
        try {
            des = new DesUtils(key);
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }//自定义密钥
        try {
            mKey = des.encrypt(mac);//加密
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        //重新写入配置文件
        file.writeList("MKEY", mKey);
        file.writeFile();*/
    }

    public void writeFile() {
        // TODO Auto-generated method stub
        String mkeyold = map.get("MKEY");
        String mac = MacUtil.getMacAddress();
        DesUtils des = null;
        String mKey = "";
        try {
            des = new DesUtils(key);
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }//自定义密钥
        try {
            mKey = des.encrypt(mac);//加密
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        //重新写入配置文件
        if(!mkeyold.equals(mKey)) {
            map.put("MKEY",mKey);
            System.out.println("配置文件写入机器码开启——————————————————————");
           fileWrite.write(fileName,map);
        }
    }

    public static void main(String [] arg0) throws IOException {
       RegisterKey re= new RegisterKey();
       re.isRegister();
    }
}
